﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using ServiceTools;
using System.IO;

namespace PeoneFS
{
    public class InstallableServiceBase : ServiceBase
    {
        protected string serviceName;
        protected string serviceDescription;
        protected TextWriter logStream;

        public InstallableServiceBase(string serviceName, string serviceDescription)
            : this(serviceName, serviceDescription, Console.Out)
        {
        }

        public InstallableServiceBase(string serviceName, string serviceDescription, TextWriter logStream)
        {
            this.serviceName = serviceName;
            this.serviceDescription = serviceDescription;
            this.logStream = logStream;
        }

        public void InstallAndStart()
        {
            try
            {
                ServiceInstallerHelper.InstallAndStart(
                    serviceName, 
                    serviceDescription, 
                    System.Reflection.Assembly.GetExecutingAssembly().Location,
                    new String[] { "runservice" });
                logStream.WriteLine("{0} service installed", this.serviceName);
            }
            catch (Exception e)
            {
                logStream.WriteLine("Error installing the service {0}. Stack trace:\n{1}", this.serviceName, e);
            }
        }

        public void UninstallService()
        {
            try
            {
                logStream.WriteLine("Stopping {0}...", this.serviceName);
                ServiceInstallerHelper.StopService(this.serviceName);
                logStream.WriteLine("successfully stopped. Uninstalling...");
                ServiceInstallerHelper.Uninstall(this.serviceName);
                logStream.WriteLine("successfully uninstalled.");
            }
            catch (Exception e)
            {
                logStream.WriteLine("Error uninstalling the service {0}. Stack trace:\n{1}", this.serviceName, e);
            }
        }

        public void StartService()
        {
            try
            {
                logStream.WriteLine("Starting...");
                ServiceInstallerHelper.StartService(this.serviceName);
                logStream.WriteLine("successfully started.");
            }
            catch (Exception e)
            {
                logStream.WriteLine("Error starting the {0} service. Stack trace:\n{1}", this.serviceName, e);
            }
        }

        public void StopService()
        {
            try
            {
                logStream.WriteLine("Stopping...");
                ServiceInstallerHelper.StopService(this.serviceName);
                logStream.WriteLine("successfully stopped.");
            }
            catch (Exception e)
            {
                logStream.WriteLine("Error stopping the {0} service. Stack trace:\n{1}", this.serviceName, e);
            }
        }

        public string Status
        {
            get
            {
                return ServiceInstallerHelper.GetServiceStatus(this.serviceName).ToString();
            }
        }
    }
}
